using System;
using System.Xml;
using System.Collections;
using System.ComponentModel;
using Widgetsphere.Generator.Common.EventArgs;

namespace Widgetsphere.Generator.Common.GeneratorFramework
{
  public delegate void MessageDelegate(object sender, MessageEventArgs e);

  public class MessageCollection : CollectionBase
  {
    #region Member Variables
    
    #endregion

    #region Constructor

    #endregion

    #region Events

    public event MessageDelegate AfterMessageAdd;
    public event MessageDelegate AfterMessageRemove;

    protected void OnAfterMessageAdd(object sender, MessageEventArgs e)
    {
      if (this.AfterMessageAdd != null)
        this.AfterMessageAdd(sender, e);
    }

    protected void OnAfterMessageRemove(object sender, MessageEventArgs e)
    {
      if (this.AfterMessageRemove != null)
        this.AfterMessageRemove(sender, e);
    }
    
    #endregion

    #region Property Implementations

    #endregion

    #region IList Members

    public Message this[int index]
    {
      get
      {
        return (Message)this.InnerList[index];
      }
      set
      {
        this.InnerList[index] = value;
      }
    }

    public new void RemoveAt(int index)
    {
      Message element = this[index];
      this.InnerList.RemoveAt(index);
      this.OnAfterMessageRemove(this, new Widgetsphere.Generator.Common.EventArgs.MessageEventArgs(element));
    }

    public void Insert(int index, Message value)
    {
      this.InnerList.Insert(index, value);
    }

    public void Remove(Message value)
    {
      this.InnerList.Remove(value);
      this.OnAfterMessageRemove(this, new Widgetsphere.Generator.Common.EventArgs.MessageEventArgs(value));
    }

    public bool Contains(Message value)
    {
      return this.InnerList.Contains(value);
    }

    public int IndexOf(Message value)
    {
      return this.InnerList.IndexOf(value);
    }

    internal int Add(Message value)
    {
      int retval = this.InnerList.Add(value);
      this.OnAfterMessageAdd(this, new Widgetsphere.Generator.Common.EventArgs.MessageEventArgs(value));
      return retval;
    }

    public Message Add(MessageTypeConstants messageType, string text, IModelObjectController controller)
    {
      Message newItem = new Message(messageType, text, controller);
      this.Add(newItem);
      return newItem;
    }

    public void AddRange(ICollection list)
    {
      this.InnerList.AddRange(list);

      foreach(Message message in list)
        this.OnAfterMessageAdd(this, new Widgetsphere.Generator.Common.EventArgs.MessageEventArgs(message));
    }

    #endregion

    #region IEnumerable Members

    public IEnumerator GetEnumerator()
    {
      return this.InnerList.GetEnumerator();
    }

    #endregion

  }
}
